<template>
  <div class="login-container">
    <SchoolLogo :base-width="160" :max-width="220" :min-width="100" :position="{ top: '3vh', left: '3vw' }" />
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="70px" class="demo-ruleForm" size="medium">
      <div class="login-header">
        <h2>{{ loginTitle }}</h2>
        <el-button type="text" @click="toggleLoginType" class="toggle-btn">
          {{ toggleButtonText }}
        </el-button>
      </div>

      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      isAdminLogin: false,
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 0, max: 20, message: '长度在 0 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 7, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  computed: {
    loginTitle() {
      return this.isAdminLogin ? '管理员登录' : '用户登录';
    },
    toggleButtonText() {
      return this.isAdminLogin ? '切换至普通用户登录' : '切换至管理员登录';
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return;
        try {
          const requestUrl = this.isAdminLogin
            ? "http://localhost:8080/AdminLogin"
            : "http://localhost:8080/Login";
          const loginRes = await axios.post(requestUrl, {
            username: this.ruleForm.username,
            password: this.ruleForm.password
          });

          if (loginRes.data.code !== 1) {
            throw new Error(loginRes.data.msg || '登录失败');
          }
          const username = this.ruleForm.username;
          const jwt = loginRes.data.data;
          await this.$store.dispatch('user/setUser', { token: jwt ,username: username});
          this.$router.push(this.isAdminLogin ? '/admin/alumnus' : '/');
          this.$message.success('登录成功');
        } catch (error) {
          console.error('登录失败:', error);
          this.$message.error(error.message || '登录失败');
        }
      });
    },
    getAlumniInfo() {
      const userRes = axios.get('http://localhost:8080/GetUserInfo', {
        params: {
          username: this.ruleForm.username,
          password: this.ruleForm.password
        }
      })
      console.log(userRes.data)
      this.$store.dispatch('user/setUser', {
        id: userRes.data.data.id,
        name: userRes.data.data.name,
        username: userRes.data.data.username
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toggleLoginType() {
      this.isAdminLogin = !this.isAdminLogin;
      this.resetForm('ruleForm');
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('../assets/backgroundpic2.png') no-repeat center center;
  background-size: cover;
}

.demo-ruleForm {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3);
  background: rgba(255, 255, 255, 0.5);
}

.login-header {
  text-align: center;
  margin-bottom: 20px;
}

.toggle-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  color: #409EFF;
  font-size: 12px;
}

.toggle-btn:hover {
  color: #66b1ff;
}

.el-form-item__content {
  display: flex;
  justify-content: space-between;
}
</style>